﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ControlRotate : MonoBehaviour
{
    public Scrollbar scrollbar;
    public AutoRotate surfaceRotate;
    public float maxValue = 0.8f;
    public float minValue = 0.2f;
    public int curMode;
    private bool playedAnim = false;
    // Use this for initialization
    void Start()
    {
        // scrollbar = GetComponent<Scrollbar>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnDrag()
    {
        if (playedAnim == true)
        {
            return;
        }

        bool play = false;
        if (scrollbar.value > maxValue)
        {
            play = surfaceRotate.StartRotate(GetRotateDir(curMode, true));

        }
        else if (scrollbar.value < minValue)
        {
            play = surfaceRotate.StartRotate(GetRotateDir(curMode, false));
        }

        if (play)
        {
            playedAnim = true;
        }
    }

    private Vector3 GetRotateDir(int mode, bool dir)
    {
        if (mode == 1)
        {
            return Vector3.forward * (dir ? -1 : 1);
        }
        else if (mode == 2)
        {
            return Vector3.right * (dir ? 1 : -1);
        }
        else
        {
            return Vector3.up * (dir ? -1 : 1);
        }
    }

    public void EndDrag()
    {
        scrollbar.value = 0.5f;
        playedAnim = false;
    }
}
